/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.RedisUtils;
import io.renren.dao.SysDictDao;
import io.renren.entity.SysDictEntity;
import io.renren.service.SysDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {


    @Autowired
    RedisUtils redisUtils;

    @Override
    public List<SysDictEntity> list(Map<String, Object> params) {
        String type = (String) params.get("type");
        String values = redisUtils.get("menu_" + type);
        if (!StringUtils.isEmpty(values)) {
            return JSON.parseArray(values, SysDictEntity.class);
        }
        List<SysDictEntity> list = this.list(new QueryWrapper<SysDictEntity>()
                .eq(StringUtils.isNotBlank(type), "type", type).eq("del_flag",0).orderByAsc("order_num"));
        if (list != null && !list.isEmpty()) {
            redisUtils.set("menu_" + type, JSON.toJSONString(list), 60 * 60 * 24);
        }
        return list;
    }

}
